<template>
  <div class="container">
    <button @click="addItem" class="btn">添加项目</button>
    <button @click="removeLastItem" class="btn1" >删除最后一项</button>
    <transition-group name="list" tag="ul" class="list-container">
      <li 
        v-for="(item, index) in items" 
        :key="item.id" 
        class="list-item"
      >
        {{ item.text }} (索引: {{ index }})
      </li>
    </transition-group>
  </div>
</template>

<script setup>
import { ref } from 'vue';

const items = ref([
  { id: 1, text: '项目1' },
  { id: 2, text: '项目2' },
  { id: 3, text: '项目3' }
]);
const addItem = () => {
  const newId = items.value.length > 0 
    ? Math.max(...items.value.map(item => item.id)) + 1 
    : 1;
  items.value.push({
    id: newId,
    text: `项目${newId}`
  });
};
const removeLastItem = () => {
  if (items.value.length > 0) {
    items.value.shift();
  }
};
</script>

<style scoped>
.btn {
  margin: 5px;
  padding: 8px 16px;
  cursor: pointer;
  color: chocolate;
}
.btn1 {
  margin: 5px;
  padding: 8px 16px;
  cursor: pointer;
  color: darkmagenta;
}
.list-container {
  list-style: none;
  padding: 0;
  margin: 20px 0;
}
.list-item {
  padding: 10px;
  margin: 5px 0;
  border: 1px solid #d34d4d;
  border-radius: 4px;
}
.list-enter-active, .list-leave-active {
  transition: all 0.5s ease;
}
.list-enter-from {
  opacity: 0;
  transform: translateX(-30px);
  background-color: aqua;
}
.list-leave-to {
  opacity: 0;
  transform: translateX(30px);
  background-color: blueviolet;
}
.list-move {
  transition: transform 0.5s ease;
}
</style>
